<template>
	<view class="">
		<view class="head">
			<view class="top">
				<image :src="infoData.image" mode="aspectFill"></image>
				<view class="top-info">
					<view class="top">
						<text class="title">{{infoData.title}}</text>
						<text class="end-time">{{infoData.deadline_time}} 报名截止</text>
					</view>
					
					<view class="top-span">
						<view class="item">{{infoData.ctitle}}</view>
					</view>
				</view>
			</view>
			
			
			<view class="add">
				<view class="li">
					<view class="left">
						<!-- <image :src="baseUrl + '/static/wximages/icon-02.png'" mode="widthFix"></image> -->
						<text>{{infoData.date}}</text>
					</view>
				</view>
				<view class="li" @click="navigateToLocation()">
					<view class="left">
						<!-- <image :src="baseUrl + '/static/wximages/icon-03.png'" mode="widthFix"></image> -->
						<text>{{infoData.address}}</text>
					</view>
					<view class="right">
						<text>距我{{distance}}km</text>
					</view>
				</view>
			</view>
			
			
			<view class="participate">
				<view class="title">当前参与</view>
				<view class="tx">
					<image :src="item.user_avatar" 
					v-for="(item, index) in infoData.record" mode="aspectFill"></image>
				</view>
			</view>
			
			
			
			
			<view class="desc">
				<!-- <view class="title">活动详情</view> -->
				<view class="desc-t">
					{{infoData.descs}}
				</view>
				
				<view class="desc-img">
					<image v-for="(item,index) in infoData.detail_img" :src="item.images" mode="widthFix"></image>
				</view>
			</view>
			
			
			<view class="btns">
				<button class="sub" @click="onSubmit">{{isjoining ? '已加入' : '立即加入'}}</button>
				<button class="fx" open-type="share">
					<image :src="baseUrl + '/static/wximages/icon-05.png'" mode="widthFix"></image>
					<text>分享</text>
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	import { getCircleInfo,circleJoining,getCircleIsJoining,userCircleBrowse } from '@/api/reserve.js';
	export default {
		data() {
			return {
				id: '',
				infoData: {},
				baseUrl: this.$baseUrl,
				isjoining: null,
				locationObj: {},
				
				distance: 0
			}
		},
		onLoad(ops) {
			if (ops.id) {
				this.id = ops.id;
			}
		},
		onShow() {
			
			this.userCircleBrowse()
			this.getCircleIsJoining()
			this.getCircleInfo()
		},
		methods: {
			getLocation() {
			    uni.getLocation({
			        type: 'gcj02', // 返回可以用于uni.openLocation的经纬度
			        success: res => {
			            console.log('当前位置信息：', res);
						this.locationObj = {
							lat: res.latitude,
							lng: res.longitude
						}
						console.log(this.infoData);
						this.distance = this.$util.distance(
						this.locationObj.lat, 
						this.locationObj.lng, 
						this.infoData.latLng.lat, 
						this.infoData.latLng.lng);
			            // 处理获取位置信息成功后的逻辑
			        },
			        fail: err => {
			            console.error('获取位置信息失败', err);
			            // 处理获取位置信息失败后的逻辑
			        }
			    });
			},
			
			getCircleInfo() {
				getCircleInfo({
					id: this.id
				}).then(res => {
					this.infoData = res.data;
					
					this.getLocation()
					
				})
			},
			getCircleIsJoining() {
				getCircleIsJoining({
					id: this.id
				}).then(res => {
					this.isjoining = res.data.isjoining;
				})
			},
			userCircleBrowse() {
				userCircleBrowse({id: this.id}).then(res =>{})
			},
			navigateToLocation() {
			  // 假设这是目的地的经纬度
			  var that = this;
			  const destination = this.infoData.latLng;
			  if (!destination) {
				  uni.showToast({
						title: '无法获取您的位置',
						icon: 'none',
				  });
				  return false;
			  }
			  uni.getLocation({
				success: function (res) {
				  uni.openLocation({
					latitude: destination.lat,
					longitude: destination.lng,
					// name: item.qiyelist[0].qiye_name,
					address: that.infoData.address,
					success() {
					  uni.showToast({
						title: '导航已启动',
						icon: 'success',
						duration: 2000,
					  });
					},
					fail() {
					  uni.showToast({
						title: '导航失败',
						icon: 'none',
					  });
					},
				  });
				},
				fail: function (error) {
				  console.log('获取位置失败', error);
				  uni.showToast({
					title: '无法获取您的位置',
					icon: 'none',
				  });
				},
			  });
			},
			onJmup(url) {
				uni.navigateTo({
					url: url
				})
			},
			onSubmit() {
				
				if (this.isjoining) {
					return this.$util.Tips({
						title: '当前活动已加入'
					});
				}
				
				uni.showLoading({
					title: '报名中'
				});
				circleJoining({
					id: this.id
				}).then(res => {
					uni.hideLoading();
					this.getCircleInfo()
					this.getCircleIsJoining()
					return this.$util.Tips({
						title: res.msg,
						icon: 'success'
					});
				}).catch(err => {
					uni.hideLoading();
					return this.$util.Tips({
						title: err
					});
				})
			}
		}
	}
</script>
<style>
	page {
		background: #003153;
	}
</style>
<style scoped lang="scss">
	
	.head {
		padding: 40rpx;
		.top {
			height: 100%;
			display: flex;
			align-items: flex-start;
			image {
				flex: 0 0 197rpx;
				width: 197rpx;
				height: 259rpx;
			}
			.top-info {
				width: 100%;
				height: 259rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				margin-left: 30rpx;
				.top {
					display: flex;
					flex-direction: column;
				}
				.title {
					font-weight: 500;
					font-size: 32rpx;
					color: #FFFFFF;
				}
				.end-time {
					font-weight: 400;
					font-size: 25rpx;
					color: #D9D9D9;
					margin-top: 20rpx;
				}
				.top-span {
					.item {
						width: 54rpx;
						height: 32rpx;
						background: #FF6915;
						border-radius: 5rpx 5rpx 5rpx 5rpx;
						font-weight: 500;
						font-size: 20rpx;
						color: #FFFFFF;
						text-align: center;
						line-height: 32rpx;
					}
				}
				
			}
		}
		
		.add {
			margin-top: 35rpx;
			.li {
				height: 60rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 10rpx;
				image {
					width: 32rpx;
					height: 32rpx;
				}
				&:nth-child(2) {
					.left {
						color: #BFDAFC;
					}
					.right {
						font-weight: 500;
						font-size: 20rpx;
						color: #BFDAFC;
					}
				}
				.left {
					display: flex;
					align-items: center;
					font-weight: 400;
					font-size: 25rpx;
					color: #FFFFFF;
					text {
						margin-left: 17rpx;
					}
				}
				.right {
					button {
						width: 45rpx;
						height: 45rpx;
						background: rgba(121,161,235,0.1);
						border-radius: 5rpx 5rpx 5rpx 5rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						image {
							width: 24rpx;
							height: 24rpx;
						}
					}
				}
				
			}
			
		}
		
		
		
		.participate {
			margin-top: 40rpx;
			.title {
				font-size: 25rpx;
				color: #FFFFFF;
				line-height: 68rpx;
			}
			.tx {
				display: flex;
				flex-wrap: wrap;
				margin-top: 10rpx;
				image {
					width: 75.5rpx;
					height: 75.5rpx;
					border-radius: 50%;
					margin-right: 24.5rpx;
					margin-bottom: 24rpx;
					&:nth-child(7n+7) {
						margin-right: 0;
					}
				}
			}
		}
		
		.desc {
			margin-top: 25rpx;
			color: #fff;
			margin-bottom: 150rpx;
			.title {
				font-size: 30rpx;
				color: #FFFFFF;
				line-height: 68rpx;
				text-align: center;
			}
			.desc-t {
				margin-bottom: 10rpx;
			}
			image {
				width: 100%;
			}
		}
		
		
		.btns {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			background-color: #003153;
			z-index: 99;
			padding: 30rpx 40rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.sub {
				width: 580rpx;
				height: 80rpx;
				background: #0B9AFF;
				border-radius: 15rpx 15rpx 15rpx 15rpx;
				font-weight: 400;
				font-size: 35rpx;
				color: #fff;
				line-height: 79rpx;
				text-align: center;
			}
			.fx {
				display: flex;
				align-items: center;
				justify-content: center;
				flex-direction: column;
				font-size: 24rpx;
				color: #FFFFFF;
				background-color: initial;
				image {
					width: 40rpx;
					height: 40rpx;
					margin-bottom: 5rpx;
				}
			}
		}
	}
</style>